<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>首页</title>
    <link rel="stylesheet" type="text/css" href="/themes/default/easyui.css">
    <link rel="stylesheet" type="text/css" href="/themes/icon.css">
    <script type="text/javascript" src="/jquery.min.js"></script>
    <script type="text/javascript" src="/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="/locale/easyui-lang-zh_CN.js"></script>

    <style type="text/css">
        table
        {
            border-collapse: collapse;
            margin: 0 auto;
            text-align: center;
        }
        table td, table th
        {
            border: 1px solid #cad9ea;
            color: #666;
            height: 30px;
        }
        table thead th
        {
            background-color: #CCE8EB;
            width: 100px;
        }
        table tr:nth-child(odd)
        {
            background: #fff;
        }
        table tr:nth-child(even)
        {
            background: #F5FAFA;
        }
    </style>
</head>
<body>
    <h1>欢迎进入首页</h1>
    <form>
        <label for="name">学生姓名:</label>
        <input type="text" id="name" name="name"><br><br>
        <label for="age">学生年龄:</label>
        <input type="number" id="age" name="age"><br><br>
        <label for="clazzid">请分配学生班级:</label>
        <select id="clazzid" name="clazzid">
            <option>请选择班级</option>
        </select><br><br>
        <input type="button" value="添加学生" onclick="insertStudent()">
    </form>
    <br><br>
    <input type="button" value="查询全部学生" onclick="selectStudent()">
    <br><br>
    <input type="button" value="查询所有班级" onclick="selectClazz()">

</body>
<script type="text/javascript">
    // 进入页面随机触发Ajax事件
    $(function () {
        $.ajax({
            url: "/boot/findClazzAll",
            type: "post",
            dataType: "json",
            success: function (data) {

                var admin = data;

//定义一个空字符串来拼接循环的内容，最后整体添加。
                var obj ='';
                for (var dict = 0; dict < admin.length; dict++) {

                    obj += "<option class='option' value='"+ admin[dict].id +"'>"+ admin[dict].name + "</option>";
                }

                $("#clazzid").append(obj);
            }
        })

    })
    // 页面绑定的按钮单击事件
    function insertStudent(){
        let name = $("#name").val();
        let age = $("#age").val();
        let clazzid = $("#clazzid").val();
        let student = {
            name: name,
            age : age,
            clazzid: clazzid
        };
        let stu = JSON.stringify(student);
        alert(stu);
        $.ajax({
            url: "/boot/insertStudent",
            type: "post",
            data: stu,
            dataType: 'json',
            contentType:'application/json;charset=utf-8',
            success: function (data){
                if (data == 1) {
                    alert("添加成功!");
                }else {
                    alert("添加失败!")
                }
            }
        })
    }
    // 查询全部学生
    function selectStudent(){
        location.href="/boot/selectStudent";
    }
    // 查询所有班级
    function selectClazz(){
        location.href="/boot/selectClazz";
    }
</script>
</html>